# -*- coding: utf-8 -*-

'''eof
name:专利数量
code:EG06_PatentIndex
tableName:
columnName:
groups:企业通用模块
dependencies:EG_BBD
type:常用指标
datasourceType:在线指标
description:
eof'''

'''将EG06企业专利记录次数 和 RP 创新指数 二者 取最大'''

import sys
import pandas as pd
import json
from datetime import datetime


def FormatData(tableName):
    try:
        ic = EG_BBD.get("srcData")[0].get("PATENT").get(tableName)
        return ic if isinstance(ic, list) else [{}]
    except:
        return [{}]


def is_num(num):
    try:
        float(num)
        return True
    except:
        return False


def getEG05():
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        depositInfo = FormatData("results")
        if depositInfo == [{}]:
            return u'缺失值'
        else:
            if len(depositInfo) >= 1:
                result_list = set()
                flag = False
                for i in depositInfo:
                    public_code = i.get("public_code")
                    patent_type = i.get("patent_type")
                    if public_code not in null_type_list and patent_type not in null_type_list:
                        flag = True
                        result_list.add(public_code + patent_type)
                return len(result_list) if flag else u'缺失值'
            else:
                return 0
    except:
        return u'缺失值'


def int_check(num):
    try:
        int(num)
        return True
    except:
        return False


def RPInnovationIndex():
    null_type_list = ['', None, 'None', 'null', 'Null', 'NULL', '/', ' ']
    try:
        interface_data = EG_BBD["srcData"][0]["OFF_RELATED_INDEX"]
        if interface_data["status"] != 200:
            return u'缺失值'
        index_codes = interface_data["data"][0]["indexCodes"]
        result_list = []
        fac1 = str(index_codes.get("395C"))
        fac2 = str(index_codes.get("444C"))
        fac3 = str(index_codes.get("161C"))
        if fac1 not in null_type_list and int_check(fac1) and int(fac1) != 99999:
            result_list.append(int(fac1))
        if fac2 not in null_type_list and int_check(fac2) and int(fac2) != 99999:
            result_list.append(int(fac2))
        if fac3 not in null_type_list and int_check(fac3) and int(fac3) != 99999:
            result_list.append(int(fac3))
        if len(result_list) >= 1:
            return sum(result_list)
        else:
            return u'缺失值'
    except:
        return u'缺失值'

def EG06_PatentIndex():
    f1 = getEG05()
    f2 = RPInnovationIndex()
    if f1 == u'缺失值' and f2 == u'缺失值':
        return u'缺失值'
    else:
        f1 = f1 if f1 != u'缺失值' else 0
        f2 = f2 if f2 != u'缺失值' else 0
        return max(f1, f2)

result = EG06_PatentIndex()




